package com.ruoyi.project.business.syscity.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.project.business.socialsecurity.request.FindLikeCityNameRequest;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.business.syscity.mapper.SysCityMapper;
import com.ruoyi.project.business.syscity.entity.SysCity;
import com.ruoyi.project.business.syscity.request.ListSysCityRequest;
import com.ruoyi.project.business.syscity.request.AddSysCityRequest;
import com.ruoyi.project.business.syscity.response.SysCityResponse;
import com.ruoyi.project.business.syscity.service.ISysCityService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ${author}
 * @date 2024-03-19
 */
@Service
public class SysCityServiceImpl implements ISysCityService {
    @Autowired
    private SysCityMapper sysCityMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @return 【请填写功能名称】
     */
    @Override
    public SysCity selectSysCityById(String code) {
        return sysCityMapper.selectSysCityById(code);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysCityResponse> selectSysCityList(ListSysCityRequest request) {
        // TODO request to  param
        SysCity param = BeanUtils.copy(request, SysCity.class);
        List<SysCity> entitis = sysCityMapper.selectSysCityList(param);
        List<SysCityResponse> resList = BeanUtils.copyList(entitis, SysCityResponse.class);
        return resList;
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @return 结果
     */
    @Override
    public int insertSysCity(AddSysCityRequest request, Long operatorId) {
        SysCity entity = BeanUtils.copy(request, SysCity.class);
        entity.setCreateTime(DateUtils.getNowDate());
        return sysCityMapper.insertSysCity(entity);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @return 结果
     */
    @Override
    public int updateSysCity(SysCity entity, Long operatorId) {
        return sysCityMapper.updateSysCity(entity);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @return 结果
     */
    @Override
    public int deleteSysCityByIds(String[] codes, Long operatorId) {
        return sysCityMapper.deleteSysCityByIds(codes);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @return 结果
     */
    @Override
    public int deleteSysCityById(String code, Long operatorId) {
        return sysCityMapper.deleteSysCityById(code);
    }

    @Override
    public List<SysCityResponse> selectListLikeName(FindLikeCityNameRequest request) {
        List<SysCity> sysCities = sysCityMapper.selectListLikeName(request.getCityName());
        List<SysCityResponse> responses = BeanUtils.copyList(sysCities, SysCityResponse.class);
        return responses;
    }

    @Override
    public Map<String, SysCity> selectMapByCityCodes(List<String> cityCodes) {
        if(CollectionUtils.isNotEmpty(cityCodes)) {
            List<SysCity> list = sysCityMapper.selectListByCodes(cityCodes);
            if(CollectionUtils.isNotEmpty(list)) {
                Map<String, SysCity> collect = list.stream().collect(Collectors.toMap(SysCity::getCode, Function.identity(), (a, b) -> a));
                return collect;
            }
        }
        return new HashMap<>();
    }

    @Override
    public Map<String, SysCity> selectMapByCityCode(String cityCode) {
        if(StringUtils.isNotEmpty(cityCode)) {
            return this.selectMapByCityCodes(Arrays.asList(cityCode));
        }
        return new HashMap<>();
    }
}
